/**
 * 工厂
 * 
 */
//创建后台数据交互工厂
aMailServices.factory('wxUser', function ($http,$timeout) {
     var wxUser = function () {
         this.busy = false;
         this.isLogin = false;
         this.isMember = false;
         this.isShow = false;
     };
     wxUser.prototype.isLoginFunc = function () {
    	 if (this.isLogin)  return;
         if (this.busy) return;
         this.busy = true;
         var pro = this;
         var url = cl.isLoginUrl+"&callback=JSON_CALLBACK" ;
         $http.jsonp(url).success(function (res) {
				if(res.code=="CL02001"){//已登录
					this.isLogin = true;
					this.initUser();
				}else if(res.code=="CL02002"){//未登陆
					this.isLogin = false;
					hideSpin();
					pro.isShow = true;
				}
				this.busy = false;
         }.bind(this));
     };
     wxUser.prototype.initUser = function(){
    	 var pro = this;
    	 var url = cl.getLoginInfoUrl+"&callback=JSON_CALLBACK" ;
         $http.jsonp(url).success(function (res) {
				if(res.code=="CL02003"){//登录失效
					this.isLogin = false;
					hideSpin();
				}else if(res.code=="CL02004"){//获取信息
					//console.info(this.items);
					cl.user.initUser(res.result);
					this.isMemberFunc();
					hideSpin();
					//console.info(this.items);
				}else if(res.code=="CL02005"){//获取信息失败
					hideSpin();
				}
//				$timeout(function(){
				pro.isShow = true;
//				},500);
         }.bind(this));
     };
     wxUser.prototype.toLogonFunc = function (successCallback,failCallback) {
         if (this.busy){
        	 hideSpin();
        	 return;
         }
         this.busy = true;
         var url = cl.isLoginUrl+"&callback=JSON_CALLBACK" ;
         $http.jsonp(url).success(function (res) {
				if(res.code=="CL02001"){//已登录
					successCallback(res.result.openid);
				}else if(res.code=="CL02002"){//未登陆
					this.isLogin = false;
					failCallback();
				}
				this.busy = false;
         }.bind(this));
     };
     wxUser.prototype.save = function(item,successCallback,failCallback){
    	 //console.info(this.busy);
    	 if (this.busy){
    		 hideSpin();
    		 return;
    	 }
         this.busy = true;
         var url = cl.saveInfoUrl+"&callback=JSON_CALLBACK";
         var params = "&user_name="+item.user_name+"&mobile_phone="+item.mobile_phone;
         $http.jsonp(url+params).success(function(res){
        	 	hideSpin();
        	 	if(res.code=="CL02003"){//登录失效
					this.isLogin = false;
				}else if(res.code=="CL02006"){//保存成功
					successCallback();
				}else if(res.code=="CL02007"){//保存失败
					failCallback();
				}
				this.busy = false;
         });
     };
     wxUser.prototype.isMemberFunc =  function(){
 		if(cl.user.mobile_phone!=""&&cl.user.mobile_phone!=undefined&&cl.user.mobile_phone!=null){
 			this.isMember = true;
 			return false;
 		}
 		this.isMember = false;
 	};
     return wxUser;
 });


/**
 * 控制器
 */
function userCtrl($scope, $routeParams,$rootScope,$location,wxUser){
	showSpin();
//	bottomMenu();
//	rightMenu();
	$scope.user = new wxUser();
	$scope.item = cl.user;
	$scope.user.isLoginFunc();
	$scope.goMyInfo = function (path, pageAnimationClass){
		showSpin();
		function success(openid){
			window.location.href = cl.goUserInfoUrl+"&openid="+openid;
		}
		function fail(){
			window.location.href=cl.wxLoginUrl;
		}
		$scope.user.toLogonFunc(success,fail);
	}
}



